package scu.debate.service.user;

import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import scu.debate.dao.DAO.UserDao;
import scu.debate.dao.entity.User;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;


@Service
public class UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String mailFrom;
    private Map<String,String> checkMap;

    public String login(String account, String password) {
        User userDO = userDao.selectByAccount(account);
        if (null == userDO) {
            return "no such user!";
        }
        String truePassword = endcodePassword(account, password);
        if (!truePassword.equals(userDO.getPassword())) {
            return "password is wrong!";
        }
        return "success";
    }

    private String endcodePassword(String account ,String password) {
        return account + password.hashCode();
    }

    public Boolean sendMail(String mailAddress){
        try{
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setSubject("freedebate注册验证");
            String code = randomCode();
            checkMap = checkCode(mailAddress, code);
            //session.setAttribute("mailAddress",mailAddress);
            //session.setAttribute("code",code);
            mailMessage.setText("您好，您正在注册freedebate,您的验证码是:"+code);
            mailMessage.setTo(mailAddress);
            mailMessage.setFrom(mailFrom);
            mailSender.send(mailMessage);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public Map<String,String> checkCode(String mailAddress,String code){
        checkMap.put("mailAddress",mailAddress);
        checkMap.put("code",code);
        return checkMap;
    }
    public void deleteCheckCode(){
        checkMap.clear();
    }

    public String randomCode(){
        String code = "";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "number";
            if ("char".equalsIgnoreCase(charOrNum)) {
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                code += (char) (choice + random.nextInt(26));
            } else if ("number".equalsIgnoreCase(charOrNum)) {
                code += String.valueOf(random.nextInt(10));
            }
        }
        return code;
    }

    public String addUser(User user,String code){
        String userEmail = user.getMailAddress();
        String mailAddress = checkMap.get("mailAddress");
        String verificationCode = checkMap.get("code");
        if(!mailAddress.equals(userEmail)){
            return "emailError";
        }else if(!code.equals(verificationCode)){
            return "codeError";
        }
        String account = user.getAccount();
        if (null != userDao.selectByAccount(account)){
            return "repeat";
        }
        userDao.insert(user);
        return JSONArray.toJSONString(user);
    }

    public String updateUser(User user){
        Integer id = user.getId();
        if (null == userDao.selectByPrimaryKey(id)){
            return "noUser";
        }
        userDao.updateByPrimaryKeySelective(user);
        return JSONArray.toJSONString(user);
    }
}
